package com.ds.web.interceptor;

import com.alibaba.fastjson.JSONObject;
import com.ds.config.JDSConfig;
import com.ds.context.JDSActionContext;
import com.ds.esd.bean.MethodConfig;
import com.ds.esd.custom.ApiClassConfig;
import com.ds.esd.custom.CustomViewFactory;
import com.ds.esd.dsm.DSMFactory;
import com.ds.web.RequestMethodBean;
import com.ds.web.RequestParamBean;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.LinkedHashSet;

@Component
public class CustomDataInterceptor extends BaseInterceptor {
    public static final String CUSSCLASSNAME = "_currClassName_";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        try {
            String projectName = this.getProjectName(request);
            MethodConfig methodConfig = CustomViewFactory.getInstance().getMethodAPIBean(request.getRequestURI(), JDSConfig.getValue("projectName"));


            Object obj = JDSActionContext.getActionContext().getParams(CUSSCLASSNAME);
            if (obj != null && !obj.equals("") && !obj.equals("RAD")) {
                String currClassName = obj.toString();
                MethodConfig topMethodConfig = CustomViewFactory.getInstance().getMethodAPIBean(currClassName, projectName);
                if (topMethodConfig != null) {
                    ApiClassConfig apiConfig = DSMFactory.getInstance().getAggregationManager().getApiClassConfig(topMethodConfig.getSourceClassName());
                    if (apiConfig != null) {
                        topMethodConfig = apiConfig.getMethodByName(topMethodConfig.getMethodName());
                    }
                    JDSActionContext.getActionContext().getContext().put(CustomViewFactory.TopMethodBeanKey, topMethodConfig);
                }
            }


            if (methodConfig != null) {
                ApiClassConfig apiConfig = DSMFactory.getInstance().getAggregationManager().getApiClassConfig(methodConfig.getSourceClassName());
                if (apiConfig != null) {
                    methodConfig = apiConfig.getMethodByName(methodConfig.getMethodName());
                }
                JDSActionContext.getActionContext().getContext().put(CustomViewFactory.MethodBeanKey, methodConfig);


                boolean json = false;
                LinkedHashSet<RequestParamBean> paramBeans = methodConfig.getParamSet();
                for (RequestParamBean paramBean : paramBeans) {
                    if (paramBean.getJsonData()) {
                        json = true;
                    }
                }

                if (json) {
                    HandlerMethod handlerMethod = (HandlerMethod) handler;
                    RequestMethodBean methodBean = findMethodBean(request.getRequestURI());
                    if (methodBean == null) {
                        methodBean = new RequestMethodBean(handlerMethod.getMethod());
                    }


                    Object object = invokMethod(methodBean, request, response);
                    if (methodBean.getResponseBody() != null) {
                        this.sendJSON(response, JSONObject.toJSONString(object));
                    } else {
                        this.sendJSON(response, object.toString());
                    }
                    return false;
                }
            }


            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }


}